// pages/customer/communicate/communicate.js
var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    isShowDialog: false,
    dateTemp: [0, 0, 0, 0, 0, 0],
    customerStatus: ['初步接触', '需求分析', '展示引导', '方案报价', '协商议价', '成单签约', '丢单'],
    customerStatuIndex: '',
    date: '',
    content: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    wx.setNavigationBarTitle({
      title: '沟通',
    });
    console.log(app.utils.formatTime(new Date()))
    this.getdate();
    this.getInfo(options.id);
    this.getCommunicateLog(options.id);
  },

  save: function() {
    var that = this;
    if (!this.data.info.customer_level && !this.data.customerStatuIndex) {
      app.showConfirm('提示', '请选择客户跟进状态', false);
    } else if (!this.data.date) {
      app.showConfirm('提示', '请选择下次联系时间', false);
    } else if (!this.data.content) {
      app.showConfirm('提示', '请输入沟通内容', false);
    } else {
      var data = {
        point_url: app.server.adminApi.createCommLog,
        admin_id: app.globalData.userInfo.id,
        member_id: this.data.info.id,
        customer_level: this.data.customerStatuIndex ? (this.data.customerStatus[this.data.customerStatuIndex]) : this.data.info.customer_level,
        comm_time: this.data.year ? (this.data.year + '-' + this.data.month + '-' + this.data.day + ' ' + this.data.hour + ':' + this.data.min + ':' + this.data.sec) : this.data.date1,
        nextcontact: this.data.date + ' 00:00:00',
        contentlog: this.data.content
      }
      console.log(data);
      app.server.getConnect(data).then(function(res) {
        console.log(res);
        wx.showToast({
          title: '保存成功',
          mask: true
        });
        setTimeout(function() {
          wx.redirectTo({
            url: '/pages/admin/plugins/customer/communicate/communicate?id=' + that.data.info.id,
          })
        }, 1000)
      }, function (res) {
        app.showConfirm('提示', res.data.msg, false);
      })
    }
  },

  getInfo: function(id) {
    var that = this;
    var data = {
      point_url: app.server.adminApi.getCustomer,
      member_id: id
    };

    app.server.getConnect(data).then(function(res) {
      console.log(res);
      that.setData({
        info: res.data[0],
        date1: app.utils.formatTime(new Date()),
      });
      wx.hideLoading();
    })
  },

  getCommunicateLog: function(id) {
    var that = this;
    var data = {
      point_url: app.server.adminApi.getCommunicateLog,
      member_id: id
    };

    app.server.getConnect(data).then(function(res) {
      that.setData({
        detailList: res.data,
      });
    })
  },

  bindCustomerStatusChange: function(e) {
    console.log(e.detail.value);
    this.setData({
      customerStatuIndex: e.detail.value
    })
  },

  bindDateChange: function(e) {
    this.setData({
      date: e.detail.value
    })
  },

  bindContent: function(e) {
    this.setData({
      content: e.detail.value
    })
  },

  getdate: function() {
    var date = new Date();
    var years = [];
    var months = [];
    var days = [];
    var hours = [];
    var mins = [];
    var secs = [];

    for (let i = 2010; i <= date.getFullYear() + 10; i++) {
      years.push(i);
    };

    for (let i = 1; i <= 12; i++) {
      if (i < 10) {
        months.push('0' + '' + i);
      } else {
        months.push(i + '');
      }
    };

    for (let i = 1; i <= 31; i++) {
      if (i < 10) {
        days.push('0' + '' + i);
      } else {
        days.push(i + '');
      }
    };

    for (let i = 1; i <= 24; i++) {
      if (i < 10) {
        hours.push('0' + '' + i);
      } else {
        hours.push(i + '');
      }
    };

    for (let i = 1; i <= 60; i++) {
      if (i < 10) {
        mins.push('0' + '' + i);
      } else {
        mins.push(i + '');
      }
    };
    this.setData({
      years: years,
      months: months,
      days: days,
      hours: hours,
      mins: mins,
      secs: secs
    })
  },

  showDialog: function() {
    this.setData({
      isShowDialog: true
    })
  },

  hiddenDialog: function() {
    this.setData({
      isShowDialog: false
    })
  },

  bindDialogChange: function(e) {
    this.setData({
      'dateTemp': e.detail.value,
    })
  },

  bindDialogConfirm: function() {
    var val = this.data.dateTemp;
    this.setData({
      isShowDialog: false,
      year: this.data.years[val[0]],
      month: this.data.months[val[1]],
      day: this.data.days[val[2]],
      hour: this.data.hours[val[3]],
      min: this.data.mins[val[4]],
      sec: this.data.mins[val[5]]
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    app.pageOnloadInit(this, app);
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    app.pageOnloadInit(this, app);
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})
